package ${basepackage}.controller;
<#assign className = table.className>
<#assign classNameLower = className?uncap_first>
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.linln.common.enums.ResultEnum;
import com.linln.common.enums.StatusEnum;
import com.linln.common.exception.ResultException;
import com.linln.common.utils.EntityBeanUtil;
import com.linln.common.utils.HttpServletUtil;
import com.linln.common.utils.ResultVoUtil;
import com.linln.common.utils.StatusUtil;
import com.linln.common.vo.ResultVo;
import com.linln.component.thymeleaf.utility.DictUtil;
import ${basepackage}.domain.${className};
import ${basepackage}.service.${className}Service;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author liz
 * @date 创建于${.now?string("yyyy-MM-dd hh:mm:SS,sss")}  <#-- freemarker特殊变量参考 http://freemarker.foofun.cn/ref_specvar.html  -->
 * ${table.remarks?if_exists}管理
 */
@Controller
@RequestMapping(value = "/system/${classNameLower}",name = "${table.remarks?if_exists}管理")
public class ${className}Controller {

    @Autowired
    private ${className}Service ${classNameLower}Service;

    /**
     * 列表页面
     */
    @GetMapping(value = "/index",name = "列表页")
    @RequiresPermissions("system:${classNameLower}:index")
    public String index(Model model, ${className} ${classNameLower}){

        int pageNum = HttpServletUtil.getParameterInt("page", PageSort.PAGE_NUMBER_DEF);
        int pageSize = HttpServletUtil.getParameterInt("size", PageSort.PAGE_SIZE_DEF);
        PageInfo<${className}> pageInfo = ${classNameLower}Service.findByPage(pageNum,pageSize,${classNameLower});

        // 封装数据
        model.addAttribute("list", null !=pageInfo && null !=pageInfo.getList() ? pageInfo.getList() : null);
        model.addAttribute("page",pageInfo);
        return "/system/${classNameLower}/index";
    }

    /**
     * 跳转到添加页面
     */
    @GetMapping(value = "/add",name = "添加页面")
    @RequiresPermissions("system:${classNameLower}:add")
    public String toAdd(){
        return "/system/${classNameLower}/add";
    }

    /**
     * 跳转到编辑页面
     */
    @GetMapping(value = "/edit/{id}",name = "编辑页面")
    @RequiresPermissions("system:${classNameLower}:edit")
    public String toEdit(@PathVariable("id") Long id, Model model){
        Dict dict = ${classNameLower}Service.getByPK(id);
        model.addAttribute("${classNameLower}", ${classNameLower});
        return "/system/${classNameLower}/add";
    }

    /**
     * 保存添加/修改的数据
     * @param valid 验证对象
     */
    @PostMapping(value = {"/add","/edit"},name = "保存或修改数据")
    @RequiresPermissions({"system:${classNameLower}:add","system:${classNameLower}:edit"})
    @ResponseBody
    public ResultVo save(${className} ${classNameLower}){
        // 复制保留无需修改的数据
        if(${classNameLower}.getId() != null){
            ${className} ${classNameLower}Old = ${classNameLower}Service.getByPK(${classNameLower}.getId());
            EntityBeanUtil.copyProperties(${classNameLower}Old, ${classNameLower});
            ${classNameLower}Service.update(${classNameLower});
        }else{
            // 新增数据
            ${classNameLower}Service.save(dict);
        }
        return ResultVoUtil.SAVE_SUCCESS;
    }

    /**
     * 跳转到详细页面
     */
    @GetMapping(value = "/detail/{id}",name = "详细页面")
    @RequiresPermissions("system:${classNameLower}:detail")
    public String toDetail(@PathVariable("id") Long id, Model model){
        ${className} ${classNameLower} = ${classNameLower}Service.getByPK(id);
        model.addAttribute(${classNameLower},${classNameLower});
        return "/system/${classNameLower}/detail";
    }

<#assign hasStatusField = false>
<#list table.columns as column>
        <#if column.sqlName?? &&  'status' == column.sqlName>
            <#assign hasStatusField = true>
        </#if>
</#list>
<#if hasStatusField>
    /**
     * 设置一条或者多条数据的状态（如果有status状态这个字段的话）
     */
    @RequestMapping(value = "/status/{param}",name = "批量改状态")
    @RequiresPermissions("system:${classNameLower}:status")
    @ResponseBody
    public ResultVo status(
            @PathVariable("param") String param,
            @RequestParam(value = "ids", required = false) List<Long> ids){
        // 更新状态
        StatusEnum statusEnum = StatusUtil.getStatusEnum(param);
        if (${classNameLower}Service.updateStatus(statusEnum, ids)) {
            return ResultVoUtil.success(statusEnum.getMessage() + "成功");
        } else {
            return ResultVoUtil.error(statusEnum.getMessage() + "失败，请重新操作");
        }
    }
</#if>
}
